package com.sumainfor.pc.epc.service.impl;

import com.sumainfor.common.Constants;
import com.sumainfor.common.exception.TCException;
import com.sumainfor.common.util.*;
import com.sumainfor.common.utlis.*;
import com.sumainfor.pc.epc.dao.*;
import com.sumainfor.pc.epc.feign.Housingauditfeign;
import com.sumainfor.pc.epc.feign.Zfeign.ZzuulFeign;
import com.sumainfor.pc.epc.service.DecorateService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.awt.*;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.locks.ReentrantLock;

@Service
@Transactional(rollbackFor = Exception.class)
public class DecorateServiceimpl implements DecorateService {

    private ReentrantLock lock = new ReentrantLock(false);//

    Logger log = LoggerFactory.getLogger(DecorateServiceimpl.class);// 日志

    @Autowired
    private DecorateMapper decorateMapper;

    @Autowired
    private BuyerManageMapper buyerManageMapper;

    @Autowired
    private Housingauditfeign houfeign;

    @Autowired
    private Tcepc_housing_plan mapper;

    @Autowired
    UtilsMapper utilsMapper;

    String UUID = null;

    @Autowired
    private ZzuulFeign zzuulFeign;


    @Autowired
    private SettlementManageMapper settlementManageMapper;

    @Autowired
    private ComUtils comUtils;

    @Autowired
    private BuyerManageServiceImpl buyerManageService;

    @Override
    public JsonResult getSummain(Map<String, Object> params) {
        JsonResult result = new JsonResult();
        Map<String, Object> map = new HashMap<String, Object>();
        BigDecimal SumSupPrice = new BigDecimal("0.00");

        BigDecimal decorate_rests = new BigDecimal("0.00");
        decorate_rests.setScale(2);
        BigDecimal sumComPrice1 = new BigDecimal("0.00");
        sumComPrice1.setScale(2);
        if (null != decorateMapper.getSumSupPrice(params.get("PlanId").toString())) {
            SumSupPrice = new BigDecimal(decorateMapper.getSumSupPrice(params.get("PlanId").toString()));
            SumSupPrice.setScale(2);
        }
        if (null != decorateMapper.getsumComPrice(params.get("PlanId").toString())) {
            decorate_rests = new BigDecimal(decorateMapper.getsumComPrice(params.get("PlanId").toString()));
            decorate_rests.setScale(2);
        }
        if (null != decorateMapper.getdecorate_rests(params.get("PlanId").toString())) {
            sumComPrice1 = new BigDecimal(decorateMapper.getdecorate_rests(params.get("PlanId").toString()));
            sumComPrice1.setScale(2);
        }
        map.put("SumSupPrice", SumSupPrice);//硬装总费用
        map.put("ComPrice", decorate_rests);//软装总费用
        map.put("restsPrice", sumComPrice1);//其他总费用
        result.put(map);
        return result;
    }

    /**
     * 按照状态分页条件查询
     *
     * @param params
     * @param pager
     * @return
     * @throws Exception
     */
    @Override
    public JsonResult getDecoratePlanpage(Map<String, Object> params, Pager pager) throws Exception {
        JsonResult result = new JsonResult();
        pager.setPagerNecessary(params, pager);
        PageUtils pageUtils = new PageUtils();
        Integer cout = decorateMapper.getCountDecoratePlanpage(params);
        List<Map<String, Object>> getCommunityList = decorateMapper.getDecoratePlanpage(params);
        List<Map<String, Object>> dataList = new ArrayList<Map<String, Object>>();
        for (Map<String, Object> map : getCommunityList) {
            map.put("couHou", decorateMapper.getcountPlansId(map.get("PlanId").toString()));
            if (map.get("PlanState").toString().equals("3")) {
                map.put("completed", decorateMapper.getcountCheckState(map.get("PlanId").toString(), 2));
                int ndecorate = Integer.parseInt(map.get("couHou").toString()) - Integer.parseInt(map.get("completed").toString());
                map.put("ndecorate", ndecorate);
                map.put("restsCount", decorateMapper.getRestsByIfStatus(map.get("PlanId").toString()));
            }
            if (map.get("PlanState").toString().equals("4")) {

                BigDecimal SumSupPrice = new BigDecimal("0.00");
                SumSupPrice.setScale(2);
                BigDecimal decorate_rests = new BigDecimal("0.00");
                decorate_rests.setScale(2);
                BigDecimal sumComPrice = new BigDecimal("0.00");
                sumComPrice.setScale(2);
                if (null != decorateMapper.getSumSupPrice(map.get("PlanId").toString())) {
                    SumSupPrice = new BigDecimal(decorateMapper.getSumSupPrice(map.get("PlanId").toString()));
                }
                if (null != decorateMapper.getdecorate_rests(map.get("PlanId").toString())) {
                    decorate_rests = new BigDecimal(decorateMapper.getdecorate_rests(map.get("PlanId").toString()));
                }
                if (null != decorateMapper.getsumComPrice(map.get("PlanId").toString())) {
                    sumComPrice = new BigDecimal(decorateMapper.getsumComPrice(map.get("PlanId").toString()));
                }
                log.info("供应商" + SumSupPrice);
                log.info("其他费用" + decorate_rests);
                log.info("软装供应商" + sumComPrice);

                BigDecimal SumSupPric = SumSupPrice.add(decorate_rests).add(sumComPrice);
                SumSupPric.setScale(2);
                map.put("SupPrice", SumSupPric);

//                if (null != decorateMapper.getSumSupPrice(map.get("PlanId").toString()) && null != decorateMapper.getdecorate_rests(map.get("PlanId").toString()) && null != decorateMapper.getsumComPrice(map.get("PlanId").toString())) {
//                    map.put("SupPrice", (decorateMapper.getSumSupPrice(map.get("PlanId").toString()) + decorateMapper.getdecorate_rests(map.get("PlanId").toString()) + decorateMapper.getsumComPrice(map.get("PlanId").toString())));
//                } else {
//                    map.put("SupPrice", 0);
//                }
            }
            dataList.add(map);
        }
        result = pageUtils.getJsonResult(dataList, params, cout);
        return result;
    }

    /**
     * 装修计划驳回之后修改重新发起
     *
     * @param params
     * @return
     */
    @Override
    public JsonResult updatedecoratePlan(Map<String, Object> params) {
        JsonResult result = new JsonResult();
        /**
         * 加锁操作
         */
        lock.lock();
        try {
            Map<String, Object> infolist = (Map<String, Object>) params.get("decorateplan");//获取主表信息
            Map<String, Object> decorateplaninfo = (Map<String, Object>) params.get("decorateplaninfo");//获取详表信息
            List<Map<String, Object>> housinglist = (List<Map<String, Object>>) params.get("housing");//获取的房源id
            List<Map<String, Object>> housinginfo = (List<Map<String, Object>>) params.get("housinginfo");//
            String HouConId = null;
            Integer HouConIds = null;
            Integer rescount = null;
            int i = 0;
            int j = 0;

            String SysUserId = ContextUtils.getUserId();
            infolist.put("HouConIds", HouConIds);
            infolist.put("by", SysUserId);//操作人
            infolist.put("term", "3");// 创建端 Insert端： 1 租客端 2市场部 3 工程部   4运营部   5财务部  6总经办 7发展部
            infolist.put("fnc",
                    this.getClass().getSimpleName() + "." + Thread.currentThread().getStackTrace()[1].getMethodName());// 创建Function名
            ParamsCommon.commonUpdateParms(infolist);
            if (decorateMapper.updateDecoratePlan(infolist) > 0) {//修改主表


                decorateplaninfo.put("HouConIds", HouConIds);
                decorateplaninfo.put("by", SysUserId);//操作人
                decorateplaninfo.put("term", "3");// 创建端 Insert端： 1 租客端 2市场部 3 工程部   4运营部   5财务部  6总经办 7发展部
                decorateplaninfo.put("fnc",
                        this.getClass().getSimpleName() + "." + Thread.currentThread().getStackTrace()[1].getMethodName());// 创建Function名
                ParamsCommon.commonUpdateParms(decorateplaninfo);
                if (decorateMapper.updateDecoratePlanInfo(decorateplaninfo) > 0) {//修改详表
                    for (Map<String, Object> map : housinglist) {
                        i++;
                        HouConId = mapper.getHouConId(map);
                        for (Map<String, Object> informap : housinginfo) {
                            informap.put("PalHouId", HouConId);//通过房源id获取HouConId

                            informap.put("by", SysUserId);//操作人
                            informap.put("term", "3");// 创建端 Insert端： 1 租客端 2市场部 3 工程部   4运营部   5财务部  6总经办 7发展部
                            informap.put("fnc",
                                    this.getClass().getSimpleName() + "." + Thread.currentThread().getStackTrace()[1].getMethodName());// 创建Function名
                            ParamsCommon.commonUpdateParms(informap);
                            if (decorateMapper.updateHousingPlanInfo(informap) > 0) {//修改无线网络有线电视
                                result.putSuccess("操作成功");
                            } else {
                                result.putFailed("操作失败");
                            }
                        }
                    }
                } else {
                    result.putFailed("操作失败");
                }
            } else {
                result.putFailed("操作失败");
            }

        } catch (Exception e) {
            log.info("e==>" + e);
            result.putFailed("服务器繁忙...");
        } finally {
            lock.unlock();//解锁
        }
        return result;
    }

    /**
     * 驳回再次添加房源
     *
     * @param params
     * @return
     */
    @Override
    public JsonResult updatedecoratePlanInfo(Map<String, Object> params) {
        JsonResult result = new JsonResult();
        String jsl = "";//自编号id
        /**
         * 加锁操作
         */
        lock.lock();
        try {
            String SysUserId = ContextUtils.getUserId();
            params.put("SysUserId", SysUserId);//装修计划负责人id

            List<Map<String, Object>> Building = (List<Map<String, Object>>) params.get("Building");//获得三大土建配置
            List<Map<String, Object>> infolist = (List<Map<String, Object>>) params.get("planinfo");//获取详表信息集合
            List<Map<String, Object>> dep = (List<Map<String, Object>>) params.get("Dep");//获得配置信息
            List<Map<String, Object>> housinglist = (List<Map<String, Object>>) params.get("housing");//获取的房源id
            List<Map<String, Object>> housinginfo = (List<Map<String, Object>>) params.get("housinginfo");//
            String HouConId = null;
            Integer HouConIds = null;
            Integer rescount = null;
            String HouB = null;
            int i = 0;
            int j = 0;
            Map<String, Object> houmap = houfeign.saveCon_Info(params);//远程调用市场部接口
            if (houmap.get("code").toString().equals("0")) {
                for (Map<String, Object> map : housinglist) {
                    i++;
                    HouConId = mapper.getHouConId(map);
                    for (Map<String, Object> informap : housinginfo) {

                        //缺少管家id
                        JsonResult usr = houfeign.getSysUserId(map.get("HouId").toString());

                        String us = (String) usr.get("result");
                        System.err.println("房源编号的" + map.get("HouId").toString() + "的管家id:" + us);

                        mapper.updateSysUserId(map.get("HouId").toString(), us);


                        informap.put("HouConId", HouConId);//通过房源id获取HouConId
                        HouConIds = mapper.getMaxHouConIds(informap);//从数据库里面查询出来最大子id
                        if (null != HouConIds) {
                            HouConIds = HouConIds + 1;
                        } else {
                            HouConIds = 1;
                        }
                        informap.put("HouConIds", HouConIds);
                        informap.put("SysUserId", us);//验房人id
                        informap.put("PlansId", jsl);//编号id;
                        informap.put("by", SysUserId);//操作人
                        informap.put("term", "3");// 创建端 Insert端： 1 租客端 2市场部 3 工程部   4运营部   5财务部  6总经办 7发展部
                        informap.put("fnc", this.getClass().getSimpleName() + "." + Thread.currentThread().getStackTrace()[1].getMethodName());// 创建Function名
                        ParamsCommon.commonInsertParms(informap);
                        rescount = mapper.saveCon_Info(informap);
                        mapper.updateIsAmend(informap);//修改状态
                        j++;
                    }
                }

                for (Map<String, Object> map : infolist) {//循环数组id
                    Map<String, Object> uuid = utilsMapper.getUUID(new HashMap<String, Object>() {{
                        put("uuid", "PlansId");
                        put("database", Constants.DATA_SOURCE_TCEPC);
                        put("surface", "tcepc_decorate_plan_info");
                    }});
                    if (uuid != null) {
                        int number = Integer.valueOf(uuid.get("uuid").toString());
                        jsl = ComUtils.getLocalTrmSeqNum("jhi", number);
                    } else {
                        jsl = ComUtils.getLocalTrmSeqNum("jhi", 0);
                    }
                    map.put("PlansId", jsl);//编号id;
                    map.put("by", SysUserId);//操作人
                    map.put("term", "3");// 创建端 Insert端： 1 租客端 2市场部 3 工程部   4运营部   5财务部  6总经办 7发展部
                    map.put("fnc", this.getClass().getSimpleName() + "." + Thread.currentThread().getStackTrace()[1].getMethodName());// 创建Function名
                    ParamsCommon.commonInsertParms(map);

                    if (decorateMapper.saveDecoratePlanInfo(map) > 0) {


                        for (Map<String, Object> Build : Building) {

                            Map<String, Object> HouBuiId = utilsMapper.getUUID(new HashMap<String, Object>() {{
                                put("uuid", "HouBuiId");
                                put("database", Constants.DATA_SOURCE_TCEPC);
                                put("surface", "tcepc_housing_plan_building");
                            }});
                            if (HouBuiId != null) {
                                int number = Integer.valueOf(HouBuiId.get("uuid").toString());
                                HouB = ComUtils.getLocalTrmSeqNum("HouB", number);
                            } else {
                                HouB = ComUtils.getLocalTrmSeqNum("HouB", 0);
                            }

                            JsonResult usr = houfeign.getSysUserId(map.get("HouId").toString());

                            String us = (String) usr.get("result");
                            System.err.println("房源编号的" + map.get("HouId").toString() + "的管家id:" + us);
                            Build.put("SysUserId", us);//管家id
                            Build.put("HouBuiId", HouB);//id
                            Build.put("PlansId", map.get("PlansId"));//装修计划自遍地
                            Build.put("HouId", map.get("HouId"));//装修计划自遍地
                            Build.put("PlansId", jsl);//编号id;
                            Build.put("by", SysUserId);//操作人
                            Build.put("term", "3");// 创建端 Insert端： 1 租客端 2市场部 3 工程部   4运营部   5财务部  6总经办 7发展部
                            Build.put("fnc", this.getClass().getSimpleName() + "." + Thread.currentThread().getStackTrace()[1].getMethodName());// 创建Function名
                            ParamsCommon.commonInsertParms(Build);//新增8大配置类型);
                            decorateMapper.savehousingplanbuilding(Build);//新增土建3达类型
                        }


                        for (Map<String, Object> dmap : dep) {
                            dmap.put("PlansId", jsl);//编号id;
                            dmap.put("by", SysUserId);//操作人
                            dmap.put("term", "3");// 创建端 Insert端： 1 租客端 2市场部 3 工程部   4运营部   5财务部  6总经办 7发展部
                            dmap.put("fnc", this.getClass().getSimpleName() + "." + Thread.currentThread().getStackTrace()[1].getMethodName());// 创建Function名
                            ParamsCommon.commonInsertParms(dmap);
                            decorateMapper.saveInfoPeploy(dmap);//新增8大配置类型
                        }
                        result.putSuccess("操作成功");
                    } else {
                        result.putFailed("操作失败");
                    }
                }
            }
        } catch (Exception e) {
            log.info("e==>" + e);
            result.putFailed("服务器繁忙...");
        } finally {
            lock.unlock();//解锁
        }
        return result;
    }

    /**
     * 创建装修计划
     *
     * @param params
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public JsonResult saveDecorate(Map<String, Object> params) {
        JsonResult result = new JsonResult();
        String jsl = "";//自编号id
        String HouB = "";
        /**
         * 加锁操作
         */
        lock.lock();
        try {
            List<Map<String, Object>> infolist = (List<Map<String, Object>>) params.get("planinfo");//获取详表信息集合
            List<Map<String, Object>> dep = (List<Map<String, Object>>) params.get("Dep");//获得配置信息
            List<Map<String, Object>> Building = (List<Map<String, Object>>) params.get("Building");//获得三大土建配置
            String SysUserId = ContextUtils.getUserId();
            params.put("SysUserId", SysUserId);//装修计划负责人id
            params.put("by", SysUserId);//操作人
            params.put("term", "3");// 创建端 Insert端： 1 租客端 2市场部 3 工程部   4运营部   5财务部  6总经办 7发展部
            params.put("fnc",
                    this.getClass().getSimpleName() + "." + Thread.currentThread().getStackTrace()[1].getMethodName());// 创建Function名
            ParamsCommon.commonInsertParms(params);
            List<Map<String, Object>> housinglist = (List<Map<String, Object>>) params.get("housing");//获取的房源id
            List<Map<String, Object>> housinginfo = (List<Map<String, Object>>) params.get("housinginfo");//
            String HouConId = null;
            Integer HouConIds = null;
            Integer rescount = null;
            int i = 0;
            int j = 0;
            Map<String, Object> houmap = houfeign.saveCon_Info(params);//远程调用市场部接口
            if (houmap.get("code").toString().equals("0")) {

                for (Map<String, Object> map : housinglist) {//循环房源id
                    i++;
                    HouConId = mapper.getHouConId(map);
                    for (Map<String, Object> informap : housinginfo) {

                        //缺少管家id
                        JsonResult usr = houfeign.getSysUserId(map.get("HouId").toString());

                        String us = (String) usr.get("result");
                        System.err.println("房源编号的" + map.get("HouId").toString() + "的管家id:" + us);

                        mapper.updateSysUserId(map.get("HouId").toString(), us);


                        informap.put("HouConId", HouConId);//通过房源id获取HouConId
                        HouConIds = mapper.getMaxHouConIds(informap);//从数据库里面查询出来最大子id
                        if (null != HouConIds) {
                            HouConIds = HouConIds + 1;
                        } else {
                            HouConIds = 1;
                        }
                        informap.put("HouConIds", HouConIds);
                        informap.put("SysUserId", us);//验房人id

                        informap.put("by", SysUserId);//操作人
                        informap.put("term", "3");// 创建端 Insert端： 1 租客端 2市场部 3 工程部   4运营部   5财务部  6总经办 7发展部
                        informap.put("fnc",
                                this.getClass().getSimpleName() + "." + Thread.currentThread().getStackTrace()[1].getMethodName());// 创建Function名
                        ParamsCommon.commonInsertParms(informap);

                        rescount = mapper.saveCon_Info(informap);
                        mapper.updateIsAmend(informap);//修改状态
                        j++;
                    }
                }

                log.info("--------------------------------------");
                log.info("params-------------------" + params);
                //判断宽带和有线电视
                log.info(params.get("Network").toString());
                log.info(params.get("Television").toString());
                log.info("--------------------------------------");

                if (decorateMapper.saveDecoratePlan(params) > 0) {//创建装修计划主表
                    log.info("主表创建成功");
                    for (Map<String, Object> map : infolist) {//循环数组id
                        Map<String, Object> uuid = utilsMapper.getUUID(new HashMap<String, Object>() {{
                            put("uuid", "PlansId");
                            put("database", Constants.DATA_SOURCE_TCEPC);
                            put("surface", "tcepc_decorate_plan_info");
                        }});
                        if (uuid != null) {
                            int number = Integer.valueOf(uuid.get("uuid").toString());
                            jsl = ComUtils.getLocalTrmSeqNum("jhi", number);
                        } else {
                            jsl = ComUtils.getLocalTrmSeqNum("jhi", 0);
                        }
                        map.put("PlansId", jsl);//编号id;
                        map.put("by", SysUserId);//操作人
                        map.put("term", "3");// 创建端 Insert端： 1 租客端 2市场部 3 工程部   4运营部   5财务部  6总经办 7发展部
                        map.put("fnc", this.getClass().getSimpleName() + "." + Thread.currentThread().getStackTrace()[1].getMethodName());// 创建Function名
                        ParamsCommon.commonInsertParms(map);

                        if (decorateMapper.saveDecoratePlanInfo(map) > 0) {

                            for (Map<String, Object> Build : Building) {

                                Map<String, Object> HouBuiId = utilsMapper.getUUID(new HashMap<String, Object>() {{
                                    put("uuid", "HouBuiId");
                                    put("database", Constants.DATA_SOURCE_TCEPC);
                                    put("surface", "tcepc_housing_plan_building");
                                }});
                                if (HouBuiId != null) {
                                    int number = Integer.valueOf(HouBuiId.get("uuid").toString());
                                    HouB = ComUtils.getLocalTrmSeqNum("HouB", number);
                                } else {
                                    HouB = ComUtils.getLocalTrmSeqNum("HouB", 0);
                                }

                                JsonResult usr = houfeign.getSysUserId(map.get("HouId").toString());

                                String us = (String) usr.get("result");
                                System.err.println("房源编号的" + map.get("HouId").toString() + "的管家id:" + us);
                                Build.put("SysUserId", us);//管家id
                                Build.put("HouBuiId", HouB);//id
                                Build.put("PlansId", map.get("PlansId"));//装修计划自遍地
                                Build.put("HouId", map.get("HouId"));//装修计划自遍地
                                Build.put("PlansId", jsl);//编号id;
                                Build.put("by", SysUserId);//操作人
                                Build.put("term", "3");// 创建端 Insert端： 1 租客端 2市场部 3 工程部   4运营部   5财务部  6总经办 7发展部
                                Build.put("fnc", this.getClass().getSimpleName() + "." + Thread.currentThread().getStackTrace()[1].getMethodName());// 创建Function名
                                ParamsCommon.commonInsertParms(Build);//新增8大配置类型);
                                decorateMapper.savehousingplanbuilding(Build);//新增土建3达类型
                            }

                            for (Map<String, Object> dmap : dep) {
                                dmap.put("PlansId", jsl);//编号id;
                                dmap.put("by", SysUserId);//操作人
                                dmap.put("term", "3");// 创建端 Insert端： 1 租客端 2市场部 3 工程部   4运营部   5财务部  6总经办 7发展部
                                dmap.put("fnc", this.getClass().getSimpleName() + "." + Thread.currentThread().getStackTrace()[1].getMethodName());// 创建Function名
                                ParamsCommon.commonInsertParms(dmap);
                                decorateMapper.saveInfoPeploy(dmap);//新增8大配置类型
                            }
                            result.putSuccess("操作成功");
                        } else {
                            result.putFailed("操作失败");
                        }

                    }
                    log.info("详表循环完毕");
                }
            } else {
                result.putFailed("操作失败");
            }

            if (result.isSuccess()) {
                //通知
                CustomMap data = CustomMap.create("MeType", 5)
                        .put("Title", "待审核装修计划")
                        .put("Content", HtmlUtil.stringToHTML("您有新的批次装修计划需审核，请及时处理！", "", Color.blue))
                        .put("by", ContextUtils.getUserId())
                        .put("IfJump", 1)
                        .put("Neturl", "/sys-engineeringmanager/decorationmanagement")
                        .put("term", 3);
                zzuulFeign.AddMessage(data);
            }

        } catch (Exception e) {
            log.info("e==>" + e);
            result.putFailed("服务器繁忙...");
        } finally {
            lock.unlock();//解锁
        }
        return result;
    }

    /**
     * 获取工程部装修计划主表uuid
     *
     * @return
     */
    @Override
    public JsonResult getuuid() {
        JsonResult result = new JsonResult();
        Map<String, Object> uuid = utilsMapper.getUUID(new HashMap<String, Object>() {{
            put("uuid", "PlanId");
            put("database", Constants.DATA_SOURCE_TCEPC);
            put("surface", "tcepc_decorate_plan");
        }});
        if (uuid != null) {
            int number = Integer.valueOf(uuid.get("uuid").toString());
            UUID = ComUtils.getLocalTrmSeqNum("jh", number);
        } else {
            UUID = ComUtils.getLocalTrmSeqNum("jh", 0);
        }
        result.put(UUID);

        return result;
    }

    /**
     * 按类型查询供应商
     *
     * @param params
     * @return
     */
    @Override
    public JsonResult getSupplier(Map<String, Object> params) {
        JsonResult result = new JsonResult();
        try {

            if ("3".equals(params.get("SupType").toString())) {
                List<Map<String, Object>> datalist = decorateMapper.getSupplier(params);
                System.err.println("软装供应商");
                result.put(datalist);
            } else {
                List<Map<String, Object>> datalist = decorateMapper.getSupplier(params);
                System.err.println("硬装供应商");
                List<Map<String, Object>> maplist = new ArrayList<Map<String, Object>>();
                String type = "";
                for (Map<String, Object> map : datalist) {
                    Map<String, Object> getmap = new HashMap<String, Object>();
                    getmap.put("SupId", map.get("SupId"));//供应商id
                    getmap.put("SupsType", map.get("SupsType"));//硬装供应商装修类型    1.毛坯2.精装3.拆装
                    switch (map.get("SupsType").toString())//
                    {
                        case "1":
                            type = "毛坯";
                            break;
                        case "2":
                            type = "拆装";
                            break;
                        case "3":
                            type = "精装";
                            break;
                    }
                    ;
                    getmap.put("UnitName", map.get("Contact") + "/" + type + "/参考价格" + map.get("SupsPrice"));
                    getmap.put("SupsPrice", map.get("SupsPrice"));
                    getmap.put("Contact", map.get("Contact"));
                    maplist.add(getmap);
                }
                result.put(maplist);

            }

        } catch (Exception e) {
            log.info("查询供应商接口报错：" + e);
            result.putFailed("服务器繁忙。。。");
        }
        return result;
    }

    /**
     * 查询已参与计划的房源
     *
     * @param params
     * @param pager
     * @return
     * @throws Exception
     */
    @Override
    public JsonResult getDecorate(Map<String, Object> params, Pager pager) throws Exception {
        JsonResult result = new JsonResult();
        /**
         * 加锁操作
         */
        lock.lock();
        try {
            pager.setPagerNecessary(params, pager);
            PageUtils pageUtils = new PageUtils();
            JsonResult jsonResult = houfeign.getHousing(params, pager);//调用市场部接口
            log.info("====>" + jsonResult);
            Map<String, Object> maplist = (Map<String, Object>) jsonResult.get("result");//接到参数
            List<Map<String, Object>> datalist = (List<Map<String, Object>>) maplist.get("dataList");//接受里面datalist
            Integer count = Integer.parseInt(maplist.get("totalCount").toString());//查到总条数
            List<Map<String, Object>> getDecoratePlanList = new ArrayList<Map<String, Object>>();
            for (Map<String, Object> getmap : datalist) {
                System.out.println(getmap);
                Map<String, Object> map = decorateMapper.getdecorateByHouId(getmap.get("HouId").toString());
                if (MapUtils.isNotEmpty(map)) {
                    getmap.put("PlanId", map.get("PlanId"));
                    getmap.put("instm", map.get("instm"));
                    getDecoratePlanList.add(getmap);
                }
            }
            result = pageUtils.getJsonResult(getDecoratePlanList, params, count);

        } catch (Exception e) {
            result.putFailed("服务器繁忙....");
            log.info("" + e);
        } finally {
            lock.unlock();//解锁
        }
        return result;
    }

    @Override
    public JsonResult selectShopNames(Map<String, Object> params) {
        JsonResult result = new JsonResult();
        try {

            result.put(decorateMapper.selectShopNames(params));

        } catch (Exception e) {
            result.putFailed("服务器繁忙。。。");
        }
        return result;
    }

    @Override
    public JsonResult selectComSpeName(Map<String, Object> params) {
        JsonResult result = new JsonResult();
        try {

            result.put(decorateMapper.selectComSpeName(params));

        } catch (Exception e) {
            result.putFailed("服务器繁忙。。。");
        }
        return result;
    }

    @Override
    public JsonResult getMonadByComSpeId(Map<String, Object> params) {
        JsonResult result = new JsonResult();
        return result.put(decorateMapper.getMonadByComSpeId(params));
    }

    /**
     * 分页查询装修计划
     *
     * @param params
     * @param pager
     * @return
     * @throws Exception
     */
    @Override
    public JsonResult getDecoratePlan(Map<String, Object> params, Pager pager) throws Exception {
        JsonResult result = new JsonResult();
        try {
            pager.setPagerNecessary(params, pager);
            PageUtils pageUtils = new PageUtils();
            Integer cout = decorateMapper.getDecoratePlanCount(params);//总记录数
            List<Map<String, Object>> getCommunityList = decorateMapper.getDecoratePlan(params);//获取数据
            List<Map<String, Object>> getDecoratePlan = new ArrayList<Map<String, Object>>();
            for (Map<String, Object> map : getCommunityList) {
                map.put("Hou", decorateMapper.getPlanInfoCountByPlanId(map.get("PlanId").toString()));//装载统计房源
                getDecoratePlan.add(map);
            }
            result = pageUtils.getJsonResult(getDecoratePlan, params, cout);
        } catch (Exception e) {
            log.info("服务器繁忙..." + e);
            result.putFailed("服务器繁忙...");
        }
        return result;
    }

    /**
     * 查询装修阶段8大配置
     *
     * @return
     */
    @Override
    public JsonResult getDep() {
        JsonResult result = new JsonResult();
        result.put(decorateMapper.getDep());
        return result;
    }

    @Override
    public JsonResult getBuilding() {
        JsonResult result = new JsonResult();
        result.put(decorateMapper.getBuilding());
        return result;
    }

    @Override
    public JsonResult getSumHouByPlanId(Map<String, Object> params) {
        JsonResult result = new JsonResult();
        /**
         * 加锁操作
         */
        lock.lock();
        try {

            List<Map<String, Object>> datalist = decorateMapper.gettcepc_decorate_plan_info(params);

            List<Map<String, Object>> datamaplist = new ArrayList<Map<String, Object>>();
            for (Map<String, Object> objectMap : datalist) {
                Map<String, Object> objectMap1 = new HashMap<String, Object>();
                objectMap1.putAll(objectMap);

                BigDecimal SupPrice = new BigDecimal("0.00");//硬装费用
                BigDecimal ComPrice = new BigDecimal("0.00");//软装费用
                BigDecimal sumPrice = new BigDecimal("0.00");//其他费用

                if (null != decorateMapper.getSupPriceBydecorate_plan_info(objectMap.get("PlansId").toString())) {//硬装费用
                    SupPrice = new BigDecimal(decorateMapper.getSupPriceBydecorate_plan_info(objectMap.get("PlansId").toString()));
                    SupPrice.setScale(2);
                    // objectMap1.put("SupPrice", decorateMapper.getSupPriceBydecorate_plan_info(objectMap.get("PlansId").toString()));
                }
                objectMap1.put("SupPrice", SupPrice);

                if (null != decorateMapper.getComPriceBytcepc_housing_plan_info(objectMap.get("PlansId").toString())) {//软装费用
                    ComPrice = new BigDecimal(decorateMapper.getComPriceBytcepc_housing_plan_info(objectMap.get("PlansId").toString()));
                    ComPrice.setScale(2);
                    //  objectMap1.put("ComPrice", decorateMapper.getComPriceBytcepc_housing_plan_info(objectMap.get("PlansId").toString()));
                }
                objectMap1.put("ComPrice", ComPrice);

                if (null != decorateMapper.getComPriceBytcepc_decorate_rests(objectMap.get("PlansId").toString())) {//其他费用
                    sumPrice = new BigDecimal(decorateMapper.getComPriceBytcepc_decorate_rests(objectMap.get("PlansId").toString()));
                    sumPrice.setScale(2);
                    //objectMap1.put("sumPrice", decorateMapper.getComPriceBytcepc_decorate_rests(objectMap.get("PlansId").toString()));
                }
                objectMap1.put("sumPrice", sumPrice);
                objectMap1.put("FitmentRealityTime", objectMap.get("FitmentRealityTime"));

//
//                if (null != decorateMapper.getSupPriceBydecorate_plan_info(objectMap.get("PlansId").toString())
//                        && null != decorateMapper.getComPriceBytcepc_housing_plan_info(objectMap.get("PlansId").toString())
//                        && null != decorateMapper.getComPriceBytcepc_decorate_rests(objectMap.get("PlansId").toString())) {
//                    objectMap1.put("sumzjg", decorateMapper.getSupPriceBydecorate_plan_info(objectMap.get("PlansId").toString()) + decorateMapper.getComPriceBytcepc_housing_plan_info(objectMap.get("PlansId").toString()) + decorateMapper.getComPriceBytcepc_decorate_rests(objectMap.get("PlansId").toString()));
//                } else {
//                    objectMap1.put("sumzjg", 0);
//                }
                BigDecimal sum = SupPrice.add(ComPrice).add(sumPrice);
                sum.setScale(2);

                objectMap1.put("sumzjg", sum);

                datamaplist.add(objectMap1);
            }
            log.info("datalist====>" + datamaplist);


            JsonResult jsonResult = houfeign.gethousingByHouId(datamaplist);
            List<Map<String, Object>> maplist = (List<Map<String, Object>>) jsonResult.get("result");//接到参数
            result.put(maplist);

        } catch (Exception e) {
            log.error("" + e);
            result.putFailed("服务器繁忙...");
        } finally {
            lock.unlock();//解锁
        }

        return result;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updatetcepc_decorate_plan(Map<String, Object> params) {
        boolean result = false;
        /**
         * 加锁操作
         */
        lock.lock();
        try {
            params.put("AuditId", ContextUtils.getUserId());//装修计划负责人id
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
            // new Date()为获取当前系统时间
            params.put("AuditTime", ConvertDateTime.getCurrentTime());
            // params.put("PlanState", 3);//状态装修计划状态     1.审核中  2被驳回  3.装修中  4.已完成
            try {
                //工程经理审核装修计划
                decorateMapper.updatetcepc_decorate_plan(params);
                if ("3".equals(params.get("PlanState").toString())) {
                    log.info("经理审核操作");
                    Map<String, Object> savaBuyermap = new HashMap<String, Object>();
                    Map<String, Object> uuid = utilsMapper.getUUID(new HashMap<String, Object>() {{//先获取uuid
                        put("uuid", "BuyId");
                        put("database", Constants.DATA_SOURCE_TCEPC);
                        put("surface", "tcepc_buyer");
                    }});
                    if (uuid != null) {
                        int number = Integer.valueOf(uuid.get("uuid").toString());
                        savaBuyermap.put("BuyId", ComUtils.getLocalTrmSeqNum("Buyer", number));
                    } else {
                        savaBuyermap.put("BuyId", ComUtils.getLocalTrmSeqNum("Buyer", 0));
                    }
                    //获取所属公司编号
                    String houId = decorateMapper.getHousingIdOne(MapUtils.getString(params, "PlanId"));
                    //调用市场部服务 获取归属公司的编号信息
                    JsonResult<Integer> corIdJson = houfeign.getCorId(houId);
                    if (!corIdJson.isSuccess()) {
                        throw new TCException(corIdJson.getErrorMessage() + ":" + SystemUtils.getLineNumber());
                    }

                    savaBuyermap.put("CorId", corIdJson.getResult());
                    savaBuyermap.put("SourceId", params.get("PlanId"));//获取装修计划id
                    savaBuyermap.put("BuyType", 1);//采购单类型   1.装修采购单   2备件采购
                    savaBuyermap.put("BuyState", 1);//采购状态   1.待采购  2采购中 3已采购  4审核中
                    savaBuyermap.put("SysUserId", ContextUtils.getUserId());//操作人
                    savaBuyermap.put("by", ContextUtils.getUserId());//操作人
                    savaBuyermap.put("term", "3");// 创建端 Insert端： 1 租客端 2市场部 3 工程部   4运营部   5财务部  6总经办 7发展部
                    savaBuyermap.put("fnc",
                            this.getClass().getSimpleName() + "." + Thread.currentThread().getStackTrace()[1].getMethodName());// 创建Function名
                    ParamsCommon.commonInsertParms(savaBuyermap);


                    if (decorateMapper.saveTcepcBuyer(savaBuyermap) > 0) {
                        //成功
                        CustomMap data = CustomMap.create("MeType", 7)
                                .put("Title", "新接收的装修采购单")
                                .put("Content", HtmlUtil.stringToHTML("您收到一条新的装修采购单，请及时处理！；", "", Color.blue))
                                .put("by", ContextUtils.getUserId())
                                .put("IfJump", 1)
                                .put("Neturl", "/sys-engineeringmanager/buyer")
                                .put("term", 3);
                        zzuulFeign.AddMessage(data);


                        //创建硬装结算单
                        //生成硬装的结算单详表数据
                        //采购单主表数据
                        Map<String, Object> buyerMainData = savaBuyermap;

                        List<Map<String, Object>> planInfoList = buyerManageMapper.selectPlanInfoListByBuyerId(MapUtils.getString(buyerMainData, "BuyId"));
                        if (CollectionUtils.isEmpty(planInfoList)) {
                            throw new TCException("找不到硬装供应商数据");
                        }
                        BigDecimal yzSum = new BigDecimal(0);
                        for (Map<String, Object> objectMap : planInfoList) {
                            yzSum = yzSum.add(ObjectUtil.convertBigDecimal(MapUtils.getString(objectMap, "SupPrice")));
                        }
                        //创建硬装结算单主表
                        String staIdYz = comUtils.getLocalTrmSeqNumByLook("Sta", Constants.DATA_SOURCE_TCEPC, "tcepc_statement", "StaId");
                        int i = settlementManageMapper.insertStaMain(ParamsCommon.commonInsertParms(CustomMap.create("StaId", staIdYz)
                                .put("StaType", 0)
                                .put("StaCut", 4)
                                .put("SourceId", buyerMainData.get("SourceId"))
                                //已付金额
                                .put("PaidPrices", 0)
                                .put("CopeWithPrices", yzSum)
                                .put("UnpaidPrices", yzSum), getClass()));
                        if (i <= 0) {
                            throw new TCException("生成结算单失败");
                        }


                        //创建硬装结算单那详表数据


                        for (Map<String, Object> map : planInfoList) {
                            String stasId = comUtils.getLocalTrmSeqNumByLook("StaInfo", Constants.DATA_SOURCE_TCEPC, "tcepc_statement_info", "StasId");
                            //构建数据
                            Map<String, Object> createSetData = ParamsCommon.commonInsertParms(CustomMap.create("StasId", stasId)
                                            .put("StaId", staIdYz)
                                            .put("SupId", MapUtils.getString(map, "SupId"))
//                            .put("BudgetPrice", MapUtils.getString(map, "SupPrice"))
                                            .put("CopeWithPrice", MapUtils.getString(map, "SupPrice"))
                                            .put("PaidPrice", 0)
                                            .put("UnpaidPrice", MapUtils.getString(map, "SupPrice"))//之前是0 , 我改成这样了 by 常亚蒙
                                            .put("State", 0)
                                            .put("StaState", 1)
                                    , getClass());
                            int row = settlementManageMapper.insertStaInfoData(createSetData);
                            if (row <= 0) {
                                throw new TCException("生成结算单失败");
                            }
                        }

                        //END

                    }
                } else if ("2".equals(params.get("PlanState").toString())) {
                    //成功
                    CustomMap data = CustomMap.create("MeType", 6)
                            .put("Title", "被驳回装修计划")
                            .put("Content", HtmlUtil.stringToHTML("(" + params.get("PlanId") + ") 的装修计划被驳回；", "", Color.red))
                            .put("by", ContextUtils.getUserId())
                            .put("IfJump", 1)
                            .put("Neturl", "/sys-engineeringmanager/decorationmanagement")
                            .put("term", 3);
                    zzuulFeign.AddMessage(data);
                    log.info("经理驳回操作");
                }
                result = true;
            } catch (Exception e) {
                e.printStackTrace();
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            }
        } finally {
            lock.unlock();//解锁
        }
        return result;
    }

    @Override
    public JsonResult getBuildings(Map<String, Object> params) {

        JsonResult result = new JsonResult();

        result.put(decorateMapper.getBuildings(params));
        return result;
    }
}
